﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections;

namespace WindowsFormsApplication3.Transaction
{
    class EmployeeTransaction
    {
       /// <summary>
       /// insert data to  employe mastertable
       /// </summary>
       /// <param name="empdatabean"></param>
        public void insertEmpData(DataBean.EmployeeDatabean empdatabean)
        {
            OleDbTransaction mytrans1;
            OleDbConnection connctn = new OleDbConnection(Program.ConnStr);
            connctn.Open();
            OleDbCommand cmd = new OleDbCommand("insert into tbl_employeemaster(employeename,employecode,gender ,visanum ,nationality ,uaeid ,passportno ,phoneno ,adress ,joiningdate ,employeetype)" +
                  "values (@pemployename ,@pemployecode , @pgender, @pvisanum, @pnationality,@puaeid, @passport, @pphoneno, @padress ,@pjoiningdate ,@pemployeetype)", connctn);
            cmd.Parameters.AddWithValue("@pemployename", empdatabean.Employename);
            cmd.Parameters.AddWithValue("@pemployecode ", empdatabean.Employeecode);
            cmd.Parameters.AddWithValue("@pgender", empdatabean.Gender);
            cmd.Parameters.AddWithValue("@pvisanum", empdatabean.Visano);
            cmd.Parameters.AddWithValue("@pnationality", empdatabean.Nationality1);
            cmd.Parameters.AddWithValue("@puaeid", empdatabean.Uaeid);
            cmd.Parameters.AddWithValue("@passport", empdatabean.Passportno);
            cmd.Parameters.AddWithValue("@pphoneno", empdatabean.Phoneno);
            cmd.Parameters.AddWithValue("@padress", empdatabean.Adress);

            cmd.Parameters.AddWithValue("@pjoiningdate", empdatabean.Joiningdate);
            cmd.Parameters.AddWithValue("@pemployeetype", empdatabean.Employetype);
            //cmd.Parameters.AddWithValue("@pemployename", empdatabean.Employename);

            mytrans1 = connctn.BeginTransaction();
            cmd.Transaction = mytrans1;
            mytrans1.Commit();
            cmd.ExecuteNonQuery();
            connctn.Close();
        }

        /// <summary>
        /// insert data to salaryemploye table
        /// </summary>
        /// <param name="emppk"></param>
        /// <param name="empdatabean"></param>
        public void insertempsalarydata(int emppk ,DataBean.EmployeeDatabean empdatabean)
        {
            OleDbTransaction mytrans1;
            OleDbConnection connctn = new OleDbConnection(Program.ConnStr);
            connctn.Open();
            OleDbCommand cmd = new OleDbCommand("insert into tbl_employesalarytable(emppk ,basic , haallowance ,taallowance ,others ,total ,incurrency) values(@pcustpk , @pbasic, @phaallowance , @ptaallowance , @pothers ,@ptotal ,@pincurrency)", connctn);
            cmd.Parameters.AddWithValue("@pcustpk ", emppk);
            cmd.Parameters.AddWithValue("@pbasic,", empdatabean.Basicsalary);
            cmd.Parameters.AddWithValue("@phaallowance", empdatabean.Ha);
            cmd.Parameters.AddWithValue("@ptaallowance", empdatabean.Ta);
            cmd.Parameters.AddWithValue("@pothers", empdatabean.Others);
            cmd.Parameters.AddWithValue("@ptotal", empdatabean.Total);

            cmd.Parameters.AddWithValue("@pincurrency", empdatabean.Incurrency);
            //cmd.Parameters.AddWithValue("@pemployename", empdatabean.Employename);

            mytrans1 = connctn.BeginTransaction();
            cmd.Transaction = mytrans1;
            mytrans1.Commit();
            cmd.ExecuteNonQuery();
            connctn.Close();
        }


/// <summary>
/// delete a row
/// used when employee details are added and exception occur at the salary table
/// done in order to avoid mistakes in database
/// </summary>
/// <param name="emppk"></param>
/// <returns></returns>
        public Boolean DeleteEmpData( int emppk)
        {
            Boolean suces = false;
            OleDbTransaction mytrans1;
            OleDbConnection connctn = new OleDbConnection(Program.ConnStr);
            connctn.Open();
            OleDbCommand cmd = new OleDbCommand("Delete * from tbl_employeemaster where employeepk =@pemployeepk", connctn);
            cmd.Parameters.AddWithValue("@pemployeepk", emppk);
            

            mytrans1 = connctn.BeginTransaction();
            cmd.Transaction = mytrans1;
            mytrans1.Commit();
            cmd.ExecuteNonQuery();
            connctn.Close();
            suces = true;

            return suces;
        }

        public Boolean changeEmployeeLocation(DataBean.EmployeeDatabean empdatabean)
        {
            Boolean suces = false;
            OleDbConnection con = new OleDbConnection(Program.ConnStr);
            con.Open();
            OleDbCommand cmd = new OleDbCommand("insert into tbl_employelocation(emppk ,location,fromdate ,todate,newvisa ,remark)" +
        "values(@pemppk ,@plocation ,@pfromdate ,@ptodate ,@pnewvisa,@premark)", con);
            cmd.Parameters.AddWithValue("@pemppk", empdatabean.Employeepk);
            cmd.Parameters.AddWithValue("@plocation", empdatabean.Emplocation);
            cmd.Parameters.AddWithValue("@pfromdate ", empdatabean.Fromdate);
            cmd.Parameters.AddWithValue("@ptodate", empdatabean.Todate);
            cmd.Parameters.AddWithValue("@pnewvisa", empdatabean.Visano);
            cmd.Parameters.AddWithValue("@premark", empdatabean.Remark);
            cmd.ExecuteNonQuery();
            con.Close();

             suces = true;

            return suces;
        }

        public Boolean insertemployeephotos(ArrayList arrylist)
        {
             Boolean suces = false;

             OleDbTransaction mytrans1;
             OleDbConnection con = new OleDbConnection(Program.ConnStr);
             con.Open();
             OleDbCommand cmd = new OleDbCommand("insert into tbl_employeeimage (emppk ,imgprofileimage,imgpasprt1,imgpasprt2 ,imgvisa ,imgdocument)"+
                 "values(@pemppk , @pimgprofileimage ,@pimgpasprt1 ,@imgpasprt2 ,@pimgvisa ,@pimgdocument) ", con);
             cmd.Parameters.AddWithValue("@pemppk ", int.Parse (arrylist[0].ToString()));
             cmd.Parameters.AddWithValue("@pimgprofileimage", arrylist[1].ToString());
             cmd.Parameters.AddWithValue("@pimgpasprt1", arrylist[2].ToString());
             cmd.Parameters.AddWithValue("@imgpasprt2", arrylist[3].ToString());
             cmd.Parameters.AddWithValue("@pimgvisa ", arrylist[4].ToString());
             cmd.Parameters.AddWithValue("@pimgdocument", arrylist[5].ToString());
             mytrans1 = con.BeginTransaction();
             cmd.Transaction = mytrans1;
             mytrans1.Commit();
             cmd.ExecuteNonQuery();
             suces = true;
            con.Close();
            

            return suces;
        }







    }  
}
